<?php
namespace Admin\Controller;
class AdminController extends CommonController{
    public function _initialize() {
        parent::_initialize();
        $this->admin = D('admin');
    }

    //管理员列表
    public function _before_index() {
        $big_menu = array(
            'title' => '添加管理员',
            'iframe' => U('Admin/add'),
            'id' => 'add',
            'width' => '500',
            'height' => '210'
        );
        $this->roleid = session('roleid');
        $this->superroleid = C('SUPERADMIN_ROLEID');
        $this->assign('big_menu', $big_menu);
        $this->list_relation =true ;
    }
	
	//增加显示
    public function _before_add() {
		if($_POST){
			 $rid=I('post.role_id');
			 $data=array(
				'username'=>I('username'),
                'role_id' => $rid,
				'password'=>I('password'),
				'email'=>I('email'),
				'reg_ip'=>get_client_ip(),
				'reg_time'=>time(),
                'scope'=>I ('scope', ''),
                'last_time'=>time(),
			 );
			 $data=$this->_before_insert($data);
			if($aid=M('admin')->add($data)){
				IS_AJAX && $this->ajaxReturn(1, L('operation_success'), '', 'add');
				$this->success(L('operation_success'));
			}else{
				IS_AJAX && $this->ajaxReturn(0, L('operation_failure'));
                $this->error(L('operation_failure'));
			}
		}else{
			$role_list = M('role')->where('status=1')->select();
            if(session('roleid')!=C('SUPERADMIN_ROLEID')){
                foreach($role_list as $k=>$v){
                    if($v['id']==C('SUPERADMIN_ROLEID')){
                        unset($role_list[$k]);
                    }
                }
            }
			$this->assign('role', $role_list);
			if (IS_AJAX) {
				$response = $this->fetch();
				$this->ajaxReturn(1, '', $response);
			} else {
				$this->display();
			}
		}
	}
	
	//密码判断
    public function _before_insert($data='') {
        if( ($data['password']=='')||(trim($data['password']=='')) ){
            unset($data['password']);
        }else{
            $data['password'] = _md5($data['password']);
        }
        return $data;
    }
	
	//编辑
    public function _before_edit() {
        if($_POST){
			 $rid=I('role_id');
			 $id=I('id');
			 $data=array(
				 'id'=>$id,
                 'role_id' => $rid,
				 'username'=>I('username'),
				 'password'=>I('password'),
				 'email'=>I('email'),
				 'last_ip'=>get_client_ip(),
				 'reg_time'=>time(),
                 'last_time'=>time(),
                 'scope'=>I ('scope', ''),
			 );
			$data=$this->_before_insert($data);
            if ($this->admin->save($data)) {
                IS_AJAX && $this->ajaxReturn(1, L('operation_success'), '', 'edit');
                $this->success(L('operation_success'));
            } else {
                IS_AJAX && $this->ajaxReturn(0, L('operation_failure'));
                $this->error(L('operation_failure'));
            }
		
		}else{
			$id=I('id');
            $level = '';
            $info = M("admin")->where(array('id'=>$id))->find();
			$role_list = M('role')->where('status=1')->select();
            foreach($role_list as $k=>$v){
                if(session('roleid')!=C('SUPERADMIN_ROLEID')){
                    if($v['id']==C('SUPERADMIN_ROLEID')){
                        unset($role_list[$k]);
                    }
                }
                if ($v['id'] == $info['role_id']){
                    $level = $v['level_area'];
                }
            }


            switch ($level){
                case 0:
                    $scope = M ('division')->where (array ('id'=>$info['scope'],'status'=>1))->find();
                    break;
                case 1:
                    $scope = M ('shop')->where (array ('id'=>$info['scope'],'status'=>1))->find();
                    break;
                default:
                    $scope = M ('AreaCate')->field('id,name as title')
                        ->where (array ('id'=>$info['scope'],'status'=>1))
                        ->find();
                    break;
            }
            $this->assign('info', $info);
			$this->assign('role', $role_list);
            $this->assign ('scope', $scope);
			if(IS_AJAX){
				$response = $this->fetch();
                $this->ajaxReturn(1, '', $response);
			}else{
				$this->display();
			}
		}
    }
		
	//用户名输入时判断
    public function ajax_check_name() {
        $name = I('username');
        $id = I('id');
        if ($this->admin->name_exists($name, $id)) {
            echo 0;
        } else {
            echo 1;
        }
    }
}